package com.hylanda.pb.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hylanda.pb.entity.DisplayConfig;
import com.hylanda.pb.model.DisplayConfigQo;
import com.hylanda.pb.service.DisplayConfigService;

@Controller
@RequestMapping("/displayconfig")
public class DisplayConfigController {
	private static Logger logger = LoggerFactory.getLogger(DisplayConfigController.class);
	@Autowired
	private DisplayConfigService displayConfigService;
	private static final String prefix = "pb/";

	@RequestMapping("/index")
	public String index() {
		return prefix+"displayconfig/index";
	}

	@RequestMapping("/new")
	public String create(ModelMap model, DisplayConfig displayConfig) {

		return prefix+"displayconfig/new";
	}

	@RequestMapping("/edit/{id}")
	public String update(ModelMap model, @PathVariable Long id) {
		DisplayConfig displayConfig = displayConfigService.findOne(id);
		model.addAttribute("displayConfig", displayConfig);
		return prefix+"displayconfig/edit";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Page<DisplayConfig> getList(DisplayConfigQo qo) {
		return displayConfigService.findPage(qo);
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request, DisplayConfig displayConfig) {
		displayConfig = displayConfigService.save(displayConfig);
		logger.info("新增->ID=" + displayConfig.getId());
		return "1";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public String update(HttpServletRequest request, DisplayConfig displayConfig) {
		int rownum = displayConfigService.update(displayConfig);
		if (rownum == 1) {
			logger.info("修改->ID=" + displayConfig.getId());
			return "1";
		} else {
			return "修改操作失败";
		}
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String delete(@PathVariable Long id) {
		int rownum = displayConfigService.delete(id);
		if (rownum == 1) {
			logger.info("删除->ID=" + id);
			return "1";
		} else {
			return "删除操作失败";
		}
	}
}
